home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DDJMAG / DDJ9207.ZIP / DFLAT.792 < prev    next >
Text File  |  1992-05-26  |  26KB  |  842 lines

  1. _C PROGRAMMING COLUMN_
  2. by Al Stevens
  3.  
  4. [LISTING ONE]
  5.  
  6. /* ----------- box.c ------------ */
  7. #include "dflat.h"
  8.  
  9. int BoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  10. {
  11.     int rtn;
  12.     CTLWINDOW *ct = GetControl(wnd);
  13.     if (ct != NULL)    {
  14.         switch (msg)    {
  15.             case SETFOCUS:
  16.             case PAINT:
  17.                 return FALSE;
  18.             case LEFT_BUTTON:
  19.             case BUTTON_RELEASED:
  20.                 return SendMessage(GetParent(wnd), msg, p1, p2);
  21.             case BORDER:
  22.                 rtn = BaseWndProc(BOX, wnd, msg, p1, p2);
  23.                 if (ct != NULL)
  24.                     if (ct->itext != NULL)
  25.                         writeline(wnd, ct->itext, 1, 0, FALSE);
  26.                 return rtn;
  27.             default:
  28.                 break;
  29.         }
  30.     }
  31.     return BaseWndProc(BOX, wnd, msg, p1, p2);
  32. }
  33.  
  34.  
  35. [LISTING TWO]
  36.  
  37. /* -------------- button.c -------------- */
  38. #include "dflat.h"
  39. void PaintMsg(WINDOW wnd, CTLWINDOW *ct, RECT *rc)
  40. {
  41.     if (isVisible(wnd))    {
  42.         if (TestAttribute(wnd, SHADOW) && cfg.mono == 0)    {
  43.             /* -------- draw the button's shadow ------- */
  44.             int x;
  45.             background = WndBackground(GetParent(wnd));
  46.             foreground = BLACK;
  47.             for (x = 1; x <= WindowWidth(wnd); x++)
  48.                 wputch(wnd, 223, x, 1);
  49.             wputch(wnd, 220, WindowWidth(wnd), 0);
  50.         }
  51.         if (ct->itext != NULL)    {
  52.             unsigned char *txt;
  53.             txt = DFcalloc(1, strlen(ct->itext)+10);
  54.             if (ct->setting == OFF)    {
  55.                 txt[0] = CHANGECOLOR;
  56.                 txt[1] = wnd->WindowColors
  57.                             [HILITE_COLOR] [FG] | 0x80;
  58.                 txt[2] = wnd->WindowColors
  59.                             [STD_COLOR] [BG] | 0x80;
  60.             }
  61.             CopyCommand(txt+strlen(txt),ct->itext,!ct->setting,
  62.                 WndBackground(wnd));
  63.             SendMessage(wnd, CLEARTEXT, 0, 0);
  64.             SendMessage(wnd, ADDTEXT, (PARAM) txt, 0);
  65.             free(txt);
  66.         }
  67.         /* --------- write the button's text ------- */
  68.         WriteTextLine(wnd, rc, 0, wnd == inFocus);
  69.     }
  70. }
  71. void LeftButtonMsg(WINDOW wnd, MESSAGE msg, CTLWINDOW *ct)
  72. {
  73.     if (cfg.mono == 0)    {
  74.         /* --------- draw a pushed button -------- */
  75.         int x;
  76.         background = WndBackground(GetParent(wnd));
  77.         foreground = WndBackground(wnd);
  78.         wputch(wnd, ' ', 0, 0);
  79.         for (x = 0; x < WindowWidth(wnd); x++)    {
  80.             wputch(wnd, 220, x+1, 0);
  81.             wputch(wnd, 223, x+1, 1);
  82.         }
  83.     }
  84.     if (msg == LEFT_BUTTON)
  85.         SendMessage(NULL, WAITMOUSE, 0, 0);
  86.     else
  87.         SendMessage(NULL, WAITKEYBOARD, 0, 0);
  88.     SendMessage(wnd, PAINT, 0, 0);
  89.     if (ct->setting == ON)
  90.         PostMessage(GetParent(wnd), COMMAND, ct->command, 0);
  91.     else
  92.         beep();
  93. }
  94. int ButtonProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  95. {
  96.     CTLWINDOW *ct = GetControl(wnd);
  97.     if (ct != NULL)    {
  98.         switch (msg)    {
  99.             case SETFOCUS:
  100.                 BaseWndProc(BUTTON, wnd, msg, p1, p2);
  101.                 p1 = 0;
  102.                 /* ------- fall through ------- */
  103.             case PAINT:
  104.                 PaintMsg(wnd, ct, (RECT*)p1);
  105.                 return TRUE;
  106.             case KEYBOARD:
  107.                 if (p1 != '\r')
  108.                     break;
  109.                 /* ---- fall through ---- */
  110.             case LEFT_BUTTON:
  111.                 LeftButtonMsg(wnd, msg, ct);
  112.                 return TRUE;
  113.             case HORIZSCROLL:
  114.                 return TRUE;
  115.             default:
  116.                 break;
  117.         }
  118.     }
  119.     return BaseWndProc(BUTTON, wnd, msg, p1, p2);
  120. }
  121.  
  122.  
  123. [LISTING THREE]
  124.  
  125. /* -------------- checkbox.c ------------ */
  126. #include "dflat.h"
  127. int CheckBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  128. {
  129.     int rtn;
  130.     CTLWINDOW *ct = GetControl(wnd);
  131.     if (ct != NULL)    {
  132.         switch (msg)    {
  133.             case SETFOCUS:
  134.                 if (!(int)p1)
  135.                     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  136.             case MOVE:
  137.                 rtn = BaseWndProc(CHECKBOX, wnd, msg, p1, p2);
  138.                 SetFocusCursor(wnd);
  139.                 return rtn;
  140.             case PAINT:    {
  141.                 char cb[] = "[ ]";
  142.                 if (ct->setting)
  143.                     cb[1] = 'X';
  144.                 SendMessage(wnd, CLEARTEXT, 0, 0);
  145.                 SendMessage(wnd, ADDTEXT, (PARAM) cb, 0);
  146.                 SetFocusCursor(wnd);
  147.                 break;
  148.             }
  149.             case KEYBOARD:
  150.                 if ((int)p1 != ' ')
  151.                     break;
  152.             case LEFT_BUTTON:
  153.                 ct->setting ^= ON;
  154.                 SendMessage(wnd, PAINT, 0, 0);
  155.                 return TRUE;
  156.             default:
  157.                 break;
  158.         }
  159.     }
  160.     return BaseWndProc(CHECKBOX, wnd, msg, p1, p2);
  161. }
  162. BOOL CheckBoxSetting(DBOX *db, enum commands cmd)
  163. {
  164.     CTLWINDOW *ct = FindCommand(db, cmd, CHECKBOX);
  165.     if (ct != NULL)
  166.         return (ct->isetting == ON);
  167.     return FALSE;
  168. }
  169.  
  170.  
  171. [LISTING FOUR]
  172.  
  173. /* -------------- combobox.c -------------- */
  174. #include "dflat.h"
  175. int ListProc(WINDOW, MESSAGE, PARAM, PARAM);
  176. int ComboProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  177. {
  178.     switch (msg)    {
  179.         case CREATE_WINDOW:
  180.             wnd->extension = CreateWindow(
  181.                         LISTBOX,
  182.                         NULL,
  183.                         wnd->rc.lf,wnd->rc.tp+1,
  184.                         wnd->ht-1, wnd->wd+1,
  185.                         NULL,
  186.                         GetParent(wnd),
  187.                         ListProc,
  188.                         HASBORDER | NOCLIP | SAVESELF);
  189.             ((WINDOW)(wnd->extension))->ct->command =
  190.                                         wnd->ct->command;
  191.             wnd->ht = 1;
  192.             wnd->rc.bt = wnd->rc.tp;
  193.             break;
  194.         case PAINT:
  195.             foreground = FrameForeground(wnd);
  196.             background = FrameBackground(wnd);
  197.             wputch(wnd, DOWNSCROLLBOX, WindowWidth(wnd), 0);
  198.             break;
  199.         case KEYBOARD:
  200.             if ((int)p1 == DN)    {
  201.                 SendMessage(wnd->extension, SETFOCUS, TRUE, 0);
  202.                 return TRUE;
  203.             }
  204.             break;
  205.         case LEFT_BUTTON:
  206.             if ((int)p1 == GetRight(wnd) + 1)
  207.                 SendMessage(wnd->extension, SETFOCUS, TRUE, 0);
  208.             break;
  209.         case CLOSE_WINDOW:
  210.             SendMessage(wnd->extension, CLOSE_WINDOW, 0, 0);
  211.             break;
  212.         default:
  213.             break;
  214.     }
  215.     return BaseWndProc(COMBOBOX, wnd, msg, p1, p2);
  216. }
  217. int ListProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  218. {
  219.     DBOX *db = GetParent(wnd)->extension;
  220.     WINDOW cwnd = ControlWindow(db, wnd->ct->command);
  221.     char text[130];
  222.     int rtn;
  223.     WINDOW currFocus;
  224.     switch (msg)    {
  225.         case CREATE_WINDOW:
  226.             wnd->ct = DFmalloc(sizeof(CTLWINDOW));
  227.             wnd->ct->setting = OFF;
  228.             break;
  229.         case SETFOCUS:
  230.             if ((int)p1 == FALSE)    {
  231.                 SendMessage(wnd, HIDE_WINDOW, 0, 0);
  232.                 wnd->ct->setting = OFF;
  233.             }
  234.             else 
  235.                 wnd->ct->setting = ON;
  236.             break;
  237.         case SHOW_WINDOW:
  238.             if (wnd->ct->setting == OFF)
  239.                 return TRUE;
  240.             break;
  241.         case BORDER:
  242.             currFocus = inFocus;
  243.             inFocus = NULL;
  244.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  245.             inFocus = currFocus;
  246.             return rtn;
  247.         case LB_SELECTION:
  248.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  249.             SendMessage(wnd, LB_GETTEXT,
  250.                             (PARAM) text, wnd->selection);
  251.             PutItemText(GetParent(wnd), wnd->ct->command, text);
  252.             SendMessage(cwnd, PAINT, 0, 0);
  253.             cwnd->TextChanged = TRUE;
  254.             return rtn;
  255.         case KEYBOARD:
  256.             switch ((int) p1)    {
  257.                 case ESC:
  258.                 case FWD:
  259.                 case BS:
  260.                     SendMessage(cwnd, SETFOCUS, TRUE, 0);
  261.                     return TRUE;
  262.                 default:
  263.                     break;
  264.             }
  265.             break;
  266.         case LB_CHOOSE:
  267.             SendMessage(cwnd, SETFOCUS, TRUE, 0);
  268.             return TRUE;
  269.         case CLOSE_WINDOW:
  270.             if (wnd->ct != NULL)
  271.                 free(wnd->ct);
  272.             wnd->ct = NULL;
  273.             break;
  274.         default:
  275.             break;
  276.     }
  277.     return DefaultWndProc(wnd, msg, p1, p2);
  278. }
  279. void PutComboListText(WINDOW wnd, enum commands cmd, char *text)
  280. {
  281.     CTLWINDOW *ct = FindCommand(wnd->extension, cmd, COMBOBOX);
  282.     if (ct != NULL)        {
  283.         WINDOW lwnd = ((WINDOW)(ct->wnd))->extension;
  284.         SendMessage(lwnd, ADDTEXT, (PARAM) text, 0);
  285.     }
  286. }
  287.  
  288.  
  289. [LISTING FIVE]
  290.  
  291. /* ------------------ msgbox.c ------------------ */
  292. #include "dflat.h"
  293. extern DBOX MsgBox;
  294. extern DBOX InputBoxDB;
  295. WINDOW CancelWnd;
  296. static int ReturnValue;
  297. int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  298. {
  299.     switch (msg)    {
  300.         case CREATE_WINDOW:
  301.             GetClass(wnd) = MESSAGEBOX;
  302.             ClearAttribute(wnd, CONTROLBOX);
  303.             break;
  304.         case KEYBOARD:
  305.             if (p1 == '\r' || p1 == ESC)
  306.                 ReturnValue = (int)p1;
  307.             break;
  308.         default:
  309.             break;
  310.     }
  311.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  312. }
  313. int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  314. {
  315.     switch (msg)    {
  316.         case CREATE_WINDOW:
  317.             GetClass(wnd) = MESSAGEBOX;
  318.             ClearAttribute(wnd, CONTROLBOX);
  319.             break;
  320.         case KEYBOARD:    {
  321.             int c = tolower((int)p1);
  322.             if (c == 'y')
  323.                 SendMessage(wnd, COMMAND, ID_OK, 0);
  324.             else if (c == 'n')
  325.                 SendMessage(wnd, COMMAND, ID_CANCEL, 0);
  326.             break;
  327.         }
  328.         default:
  329.             break;
  330.     }
  331.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  332. }
  333. int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  334. {
  335.     switch (msg)    {
  336.         case CREATE_WINDOW:
  337.             GetClass(wnd) = ERRORBOX;
  338.             break;
  339.         case KEYBOARD:
  340.             if (p1 == '\r' || p1 == ESC)
  341.                 ReturnValue = (int)p1;
  342.             break;
  343.         default:
  344.             break;
  345.     }
  346.     return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
  347. }
  348. int CancelBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  349. {
  350.     switch (msg)    {
  351.         case CREATE_WINDOW:
  352.             CancelWnd = wnd;
  353.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  354.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  355.             break;
  356.         case COMMAND:
  357.             if ((int) p1 == ID_CANCEL && (int) p2 == 0)
  358.                 SendMessage(GetParent(wnd), msg, p1, p2);
  359.             return TRUE;
  360.         case CLOSE_WINDOW:
  361.             CancelWnd = NULL;
  362.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  363.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  364.             p1 = TRUE;
  365.             break;
  366.         default:
  367.             break;
  368.     }
  369.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  370. }
  371. void CloseCancelBox(void)
  372. {
  373.     if (CancelWnd != NULL)
  374.         SendMessage(CancelWnd, CLOSE_WINDOW, 0, 0);
  375. }
  376. static char *InputText;
  377. static int TextLength;
  378. int InputBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  379. {
  380.     int rtn;
  381.     switch (msg)    {
  382.         case CREATE_WINDOW:
  383.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  384.             SendMessage(ControlWindow(&InputBoxDB,ID_INPUTTEXT),
  385.                         SETTEXTLENGTH, TextLength, 0);
  386.             return rtn;
  387.         case COMMAND:
  388.             if ((int) p1 == ID_OK && (int) p2 == 0)
  389.                 GetItemText(wnd, ID_INPUTTEXT, InputText, TextLength);
  390.             break;
  391.         default:
  392.             break;
  393.     }
  394.     return DefaultWndProc(wnd, msg, p1, p2);
  395. }
  396. BOOL InputBox(WINDOW wnd,char *ttl,char *msg,char *text,int len)
  397. {
  398.     InputText = text;
  399.     TextLength = len;
  400.     InputBoxDB.dwnd.title = ttl;
  401.     InputBoxDB.dwnd.w = 4 + 
  402.         max(20, max(len, max(strlen(ttl), strlen(msg))));
  403.     InputBoxDB.ctl[1].dwnd.x = (InputBoxDB.dwnd.w-2-len)/2;
  404.     InputBoxDB.ctl[0].dwnd.w = strlen(msg);
  405.     InputBoxDB.ctl[0].itext = msg;
  406.     InputBoxDB.ctl[1].dwnd.w = len;
  407.     InputBoxDB.ctl[2].dwnd.x = (InputBoxDB.dwnd.w - 20) / 2;
  408.     InputBoxDB.ctl[3].dwnd.x = InputBoxDB.ctl[2].dwnd.x + 10;
  409.     InputBoxDB.ctl[2].isetting = ON;
  410.     InputBoxDB.ctl[3].isetting = ON;
  411.     return DialogBox(wnd, &InputBoxDB, TRUE, InputBoxProc);
  412. }
  413.  
  414. BOOL GenericMessage(WINDOW wnd,char *ttl,char *msg,int buttonct,
  415.       int (*wndproc)(struct window *,enum messages,PARAM,PARAM),
  416.       char *b1, char *b2, int c1, int c2, int isModal)
  417. {
  418.     BOOL rtn;
  419.     MsgBox.dwnd.title = ttl;
  420.     MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
  421.     MsgBox.ctl[0].dwnd.w = max(max(MsgWidth(msg),
  422.             buttonct*8 + buttonct + 2), strlen(ttl)+2);
  423.     MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
  424.     MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
  425.     if (buttonct == 1)
  426.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
  427.     else    {
  428.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
  429.         MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
  430.         MsgBox.ctl[2].class = BUTTON;
  431.     }
  432.     MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
  433.     MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
  434.     MsgBox.ctl[0].itext = msg;
  435.     MsgBox.ctl[1].itext = b1;
  436.     MsgBox.ctl[2].itext = b2;
  437.     MsgBox.ctl[1].command = c1;
  438.     MsgBox.ctl[2].command = c2;
  439.     MsgBox.ctl[1].isetting = ON;
  440.     MsgBox.ctl[2].isetting = ON;
  441.     rtn = DialogBox(wnd, &MsgBox, isModal, wndproc);
  442.     MsgBox.ctl[2].class = 0;
  443.     return rtn;
  444. }
  445. WINDOW MomentaryMessage(char *msg)
  446. {
  447.     WINDOW wnd = CreateWindow(
  448.                     TEXTBOX,
  449.                     NULL,
  450.                     -1,-1,MsgHeight(msg)+2,MsgWidth(msg)+2,
  451.                     NULL,NULL,NULL,
  452.                     HASBORDER | SHADOW | SAVESELF);
  453.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  454.     if (cfg.mono == 0)    {
  455.         WindowClientColor(wnd, WHITE, GREEN);
  456.         WindowFrameColor(wnd, WHITE, GREEN);
  457.     }
  458.     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  459.     return wnd;
  460. }
  461. int MsgHeight(char *msg)
  462. {
  463.     int h = 1;
  464.     while ((msg = strchr(msg, '\n')) != NULL)    {
  465.         h++;
  466.         msg++;
  467.     }
  468.     return min(h, SCREENHEIGHT-10);
  469. }
  470. int MsgWidth(char *msg)
  471. {
  472.     int w = 0;
  473.     char *cp = msg;
  474.     while ((cp = strchr(msg, '\n')) != NULL)    {
  475.         w = max(w, (int) (cp-msg));
  476.         msg = cp+1;
  477.     }
  478.     return min(max(strlen(msg),w), SCREENWIDTH-10);
  479. }
  480.  
  481.  
  482. [LISTING SIX]
  483.  
  484. /* -------- radio.c -------- */
  485. #include "dflat.h"
  486. static CTLWINDOW *rct[MAXRADIOS];
  487. int RadioButtonProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  488. {
  489.     int rtn;
  490.     DBOX *db = GetParent(wnd)->extension;
  491.     CTLWINDOW *ct = GetControl(wnd);
  492.     if (ct != NULL)    {
  493.         switch (msg)    {
  494.             case SETFOCUS:
  495.                 if (!(int)p1)
  496.                     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  497.             case MOVE:
  498.                 rtn = BaseWndProc(RADIOBUTTON,wnd,msg,p1,p2);
  499.                 SetFocusCursor(wnd);
  500.                 return rtn;
  501.             case PAINT:    {
  502.                 char rb[] = "( )";
  503.                 if (ct->setting)
  504.                     rb[1] = 7;
  505.                 SendMessage(wnd, CLEARTEXT, 0, 0);
  506.                 SendMessage(wnd, ADDTEXT, (PARAM) rb, 0);
  507.                 SetFocusCursor(wnd);
  508.                 break;
  509.             }
  510.             case KEYBOARD:
  511.                 if ((int)p1 != ' ')
  512.                     break;
  513.             case LEFT_BUTTON:
  514.                 PushRadioButton(db, ct->command);
  515.                 break;
  516.             default:
  517.                 break;
  518.         }
  519.     }
  520.     return BaseWndProc(RADIOBUTTON, wnd, msg, p1, p2);
  521. }
  522. void PushRadioButton(DBOX *db, enum commands cmd)
  523. {
  524.     CTLWINDOW *ct = FindCommand(db, cmd, RADIOBUTTON);
  525.     if (ct != NULL)    {
  526.         SetRadioButton(db, ct);
  527.         ct->isetting = ON;
  528.     }
  529. }
  530. void SetRadioButton(DBOX *db, CTLWINDOW *ct)
  531. {
  532.     CTLWINDOW *ctt = db->ctl;
  533.     int i;
  534.     /* --- clear all the radio buttons in this group on the dialog box --- */
  535.     /* -------- build a table of all radio buttons at the
  536.             same x vector ---------- */
  537.     for (i = 0; i < MAXRADIOS; i++)
  538.         rct[i] = NULL;
  539.     while (ctt->class)    {
  540.         if (ctt->class == RADIOBUTTON)
  541.             if (ct->dwnd.x == ctt->dwnd.x)
  542.                 rct[ctt->dwnd.y] = ctt;
  543.         ctt++;
  544.     }
  545.     /* ----- find the start of the radiobutton group ---- */
  546.     i = ct->dwnd.y;
  547.     while (i >= 0 && rct[i] != NULL)
  548.         --i;
  549.     /* ---- ignore everthing before the group ------ */
  550.     while (i >= 0)
  551.         rct[i--] = NULL;
  552.     /* ----- find the end of the radiobutton group ---- */
  553.     i = ct->dwnd.y;
  554.     while (i < MAXRADIOS && rct[i] != NULL)
  555.         i++;
  556.     /* ---- ignore everthing past the group ------ */
  557.     while (i < MAXRADIOS)
  558.         rct[i++] = NULL;
  559.     for (i = 0; i < MAXRADIOS; i++)    {
  560.         if (rct[i] != NULL)    {
  561.             int wason = rct[i]->setting;
  562.             rct[i]->setting = OFF;
  563.             if (wason)
  564.                 SendMessage(rct[i]->wnd, PAINT, 0, 0);
  565.         }
  566.     }
  567.     ct->setting = ON;
  568.     SendMessage(ct->wnd, PAINT, 0, 0);
  569. }
  570. BOOL RadioButtonSetting(DBOX *db, enum commands cmd)
  571. {
  572.     CTLWINDOW *ct = FindCommand(db, cmd, RADIOBUTTON);
  573.     if (ct != NULL)
  574.         return (ct->setting == ON);
  575.     return FALSE;
  576. }
  577.  
  578.  
  579. [LISTING SEVEN]
  580.  
  581. /* ------------- slidebox.c ------------ */
  582. #include "dflat.h"
  583. static int (*GenericProc)
  584.     (WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2);
  585. static BOOL KeepRunning;
  586. static int SliderLen;
  587. static int Percent;
  588. extern DBOX SliderBoxDB;
  589. static void InsertPercent(char *s)
  590. {
  591.     int offset;
  592.     char pcc[5];
  593.  
  594.     sprintf(s, "%c%c%c",
  595.             CHANGECOLOR,
  596.             color[DIALOG][SELECT_COLOR][FG]+0x80,
  597.             color[DIALOG][SELECT_COLOR][BG]+0x80);
  598.     s += 3;
  599.     memset(s, ' ', SliderLen);
  600.     *(s+SliderLen) = '\0';
  601.     sprintf(pcc, "%d%%", Percent);
  602.     strncpy(s+SliderLen/2-1, pcc, strlen(pcc));
  603.     offset = (SliderLen * Percent) / 100;
  604.     memmove(s+offset+4, s+offset, strlen(s+offset)+1);
  605.     sprintf(pcc, "%c%c%c%c",
  606.             RESETCOLOR,
  607.             CHANGECOLOR,
  608.             color[DIALOG][SELECT_COLOR][BG]+0x80,
  609.             color[DIALOG][SELECT_COLOR][FG]+0x80);
  610.     strncpy(s+offset, pcc, 4);
  611.     *(s + strlen(s) - 1) = RESETCOLOR;
  612. }
  613. static int SliderTextProc(
  614.             WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
  615. {
  616.     switch (msg)    {
  617.         case PAINT:
  618.             Percent = (int)p2;
  619.             InsertPercent(GetText(wnd) ?
  620.                 GetText(wnd) : SliderBoxDB.ctl[1].itext);
  621.             GenericProc(wnd, PAINT, 0, 0);
  622.             if (Percent >= 100)
  623.                 SendMessage(GetParent(wnd),COMMAND,ID_CANCEL,0);
  624.             if (!dispatch_message())
  625.                 PostMessage(GetParent(wnd), ENDDIALOG, 0, 0);
  626.             return KeepRunning;
  627.         default:
  628.             break;
  629.     }
  630.     return GenericProc(wnd, msg, p1, p2);
  631. }
  632. static int SliderBoxProc(
  633.             WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  634. {
  635.     int rtn;
  636.     WINDOW twnd;
  637.     switch (msg)    {
  638.         case CREATE_WINDOW:
  639.             AddAttribute(wnd, SAVESELF);
  640.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  641.             twnd = SliderBoxDB.ctl[1].wnd;
  642.             GenericProc = twnd->wndproc;
  643.             twnd->wndproc = SliderTextProc;
  644.             KeepRunning = TRUE;
  645.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  646.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  647.             return rtn;
  648.         case COMMAND:
  649.             if ((int)p2 == 0 && (int)p1 == ID_CANCEL)    {
  650.                 if (Percent >= 100 ||
  651.                         YesNoBox("Terminate process?"))
  652.                     KeepRunning = FALSE;
  653.                 else
  654.                     return TRUE;
  655.             }
  656.             break;
  657.         case CLOSE_WINDOW:
  658.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  659.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  660.             break;
  661.         default:
  662.             break;
  663.     }
  664.     return DefaultWndProc(wnd, msg, p1, p2);
  665. }
  666. WINDOW SliderBox(int len, char *ttl, char *msg)
  667. {
  668.     SliderLen = len;
  669.     SliderBoxDB.dwnd.title = ttl;
  670.     SliderBoxDB.dwnd.w =
  671.         max(strlen(ttl),max(len, strlen(msg)))+4;
  672.     SliderBoxDB.ctl[0].itext = msg;
  673.     SliderBoxDB.ctl[0].dwnd.w = strlen(msg);
  674.     SliderBoxDB.ctl[0].dwnd.x =
  675.         (SliderBoxDB.dwnd.w - strlen(msg)-1) / 2;
  676.     SliderBoxDB.ctl[1].itext =
  677.         DFrealloc(SliderBoxDB.ctl[1].itext, len+10);
  678.     Percent = 0;
  679.     InsertPercent(SliderBoxDB.ctl[1].itext);
  680.     SliderBoxDB.ctl[1].dwnd.w = len;
  681.     SliderBoxDB.ctl[1].dwnd.x = (SliderBoxDB.dwnd.w-len-1)/2;
  682.     SliderBoxDB.ctl[2].dwnd.x = (SliderBoxDB.dwnd.w-10)/2;
  683.     DialogBox(NULL, &SliderBoxDB, FALSE, SliderBoxProc);
  684.     return SliderBoxDB.ctl[1].wnd;
  685. }
  686.  
  687.  
  688. [LISTING EIGHT]
  689.  
  690. /* ------------ spinbutt.c ------------- */
  691. #include "dflat.h"
  692. int SpinButtonProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  693. {
  694.     int rtn;
  695.     CTLWINDOW *ct = GetControl(wnd);
  696.     if (ct != NULL)    {
  697.         switch (msg)    {
  698.             case CREATE_WINDOW:
  699.                 wnd->wd -= 2;
  700.                 wnd->rc.rt -= 2;
  701.                 break;
  702.             case SETFOCUS:
  703.                 rtn = BaseWndProc(SPINBUTTON, wnd, msg, p1, p2);
  704.                 if (!(int)p1)
  705.                     SendMessage(NULL, HIDE_CURSOR, 0, 0);
  706.                 SetFocusCursor(wnd);
  707.                 return rtn;
  708.             case PAINT:
  709.                 foreground = FrameForeground(wnd);
  710.                 background = FrameBackground(wnd);
  711.                 wputch(wnd,UPSCROLLBOX,WindowWidth(wnd), 0);
  712.                 wputch(wnd,DOWNSCROLLBOX,WindowWidth(wnd)+1,0);
  713.                 SetFocusCursor(wnd);
  714.                 break;
  715.             case LEFT_BUTTON:
  716.                 if (p1 == GetRight(wnd) + 1)
  717.                     SendMessage(wnd, KEYBOARD, UP, 0);
  718.                 else if (p1 == GetRight(wnd) + 2)
  719.                     SendMessage(wnd, KEYBOARD, DN, 0);
  720.                 if (wnd != inFocus)
  721.                     SendMessage(wnd, SETFOCUS, TRUE, 0);
  722.                 return TRUE;
  723.             case LB_SETSELECTION:
  724.                 rtn = BaseWndProc(SPINBUTTON, wnd, msg, p1, p2);
  725.                 wnd->wtop = (int) p1;
  726.                 SendMessage(wnd, PAINT, 0, 0);
  727.                 return rtn;
  728.             default:
  729.                 break;
  730.         }
  731.     }
  732.     return BaseWndProc(SPINBUTTON, wnd, msg, p1, p2);
  733. }
  734.  
  735.  
  736.  
  737. [LISTING NINE]
  738.  
  739. /* -------------- text.c -------------- */
  740. #include "dflat.h"
  741. int TextProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  742. {
  743.     int i, len;
  744.     CTLWINDOW *ct = GetControl(wnd);
  745.     char *cp, *cp2 = ct->itext;
  746.     switch (msg)    {
  747.         case PAINT:
  748.             if (ct == NULL ||
  749.                 ct->itext == NULL ||
  750.                     GetText(wnd) != NULL)
  751.                 break;
  752.             len = min(ct->dwnd.h, MsgHeight(cp2));
  753.             cp = cp2;
  754.             for (i = 0; i < len; i++)    {
  755.                 int mlen;
  756.                 char *txt = cp;
  757.                 char *cp1 = cp;
  758.                 char *np = strchr(cp, '\n');
  759.                 if (np != NULL)
  760.                     *np = '\0';
  761.                 mlen = strlen(cp);
  762.                 while ((cp1=strchr(cp1,SHORTCUTCHAR)) != NULL) {
  763.                     mlen += 3;
  764.                     cp1++;
  765.                 }
  766.                 if (np != NULL)
  767.                     *np = '\n';
  768.                 txt = DFmalloc(mlen+1);
  769.                  CopyCommand(txt, cp, FALSE, WndBackground(wnd));
  770.                 txt[mlen] = '\0';
  771.                 SendMessage(wnd, ADDTEXT, (PARAM)txt, 0);
  772.                 if ((cp = strchr(cp, '\n')) != NULL)
  773.                     cp++;
  774.                 free(txt);
  775.             }
  776.             break;
  777.         default:
  778.             break;
  779.     }
  780.     return BaseWndProc(TEXT, wnd, msg, p1, p2);
  781. }
  782.  
  783.  
  784. [LISTING TEN]
  785.  
  786. /* ----------- watch.c ----------- */
  787.  
  788. #include "dflat.h"
  789.  
  790. static int WatchIconProc(
  791.                 WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  792. {
  793.     int rtn;
  794.     switch (msg)    {
  795.         case CREATE_WINDOW:
  796.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  797.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  798.             SendMessage(wnd, HIDE_MOUSE, 0, 0);
  799.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  800.             return rtn;
  801.         case PAINT:
  802.             SetStandardColor(wnd);
  803.             writeline(wnd, " @ ", 1, 1, FALSE);
  804.             return TRUE;
  805.         case BORDER:
  806.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  807.             writeline(wnd, "M", 2, 0, FALSE);
  808.             return rtn;
  809.         case MOUSE_MOVED:
  810.             SendMessage(wnd, HIDE_WINDOW, TRUE, 0);
  811.             SendMessage(wnd, MOVE, p1, p2);
  812.             SendMessage(wnd, SHOW_WINDOW, 0, 0);
  813.             return TRUE;
  814.         case CLOSE_WINDOW:
  815.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  816.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  817.             SendMessage(wnd, SHOW_MOUSE, 0, 0);
  818.             break;
  819.         default:
  820.             break;
  821.     }
  822.     return DefaultWndProc(wnd, msg, p1, p2);
  823. }
  824.  
  825. WINDOW WatchIcon(void)
  826. {
  827.     int mx, my;
  828.     WINDOW wnd;
  829.     SendMessage(NULL, CURRENT_MOUSE_CURSOR,
  830.                         (PARAM) &mx, (PARAM) &my);
  831.     wnd = CreateWindow(
  832.                     BOX,
  833.                     NULL,
  834.                     mx, my, 3, 5,
  835.                     NULL,NULL,
  836.                     WatchIconProc,
  837.                     VISIBLE | HASBORDER | SHADOW | SAVESELF);
  838.     return wnd;
  839. }
  840.  
  841.  
  842.